[Spring MVC] details the various parameter bindings of SpringMVC

  • 2020-05-24 05:38:09
  • OfStack

Various parameter binding methods of SpringMVC

1. Basic data types (take int for example, others are similar) :

Controller code:


@RequestMapping("saysth.do")
public void test(int count) {
}

Form code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>

If the name value of input in the form and the parameter variable name of Controller are kept at 1, the data binding can be completed. If the value of input is not 1, you can use the @RequestParam annotation. Note that if the basic data type is defined in the Controller method parameter, but the data submitted from the page is null or "", there will be an exception to the data conversion. In other words, you must ensure that the data passed by the form cannot be null or "". Therefore, it is better to define the parameter data type as the wrapper type for the data that may be empty during the development process. See the following example for details.

2. Packaging type (take Integer for example, other similar) :

Controller code:


@RequestMapping("saysth.do")
public void test(Integer count) {
}

Form code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>

Unlike basic data type 1, the data passed from the form can be null or "". For example, if num in the form is" "or num is not input in the form, then num value in the Controller method parameter is null.

3. Custom object type:

Model code:


public class User {
  private String firstName;
  private String lastName;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

}

Controller code:


@RequestMapping("saysth.do")
public void test(User user) {
}

Form code:


<form action="saysth.do" method="post">
<input name="firstName" value=" zhang " type="text"/>
<input name="lastName" value="3" type="text"/>
......
</form>

It's as simple as matching the property name of the object with the name value of input, 11.

4. Custom composite object type:

Model code:


public class ContactInfo {
  private String tel;
  private String address;

  public String getTel() {
    return tel;
  }

  public void setTel(String tel) {
    this.tel = tel;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

}

public class User {
  private String firstName;
  private String lastName;
  private ContactInfo contactInfo;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public ContactInfo getContactInfo() {
    return contactInfo;
  }

  public void setContactInfo(ContactInfo contactInfo) {
    this.contactInfo = contactInfo;
  }

}

Controller code:


@RequestMapping("saysth.do")
public void test(User user) {
  System.out.println(user.getFirstName());
  System.out.println(user.getLastName());
  System.out.println(user.getContactInfo().getTel());
  System.out.println(user.getContactInfo().getAddress());
}

Form code:


<form action="saysth.do" method="post">
<input name="firstName" value=" zhang " /><br>
<input name="lastName" value="3" /><br>
<input name="contactInfo.tel" value="13809908909" /><br>
<input name="contactInfo.address" value=" Beijing haidian " /><br>
<input type="submit" value="Save" />
</form>

User objects have ContactInfo properties, and the code in Controller corresponds to the 1 in point 3, but in the form code, you need to name name of input using "property name (property of the object type). Property name."

5. List binding:

List needs to be bound to an object and cannot be written directly into the parameters of the Controller method.

Model code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
0

Controller code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
1

Form code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
2

In fact, this is similar to the binding of contantInfo data in the User object at point 4, but the properties in the UserListForm object are defined as List instead of a custom object. Therefore, you need to specify the subscript of List in the form. Is worth 1, Spring will create one with the largest values for size List object, so, if the form is dynamically add, delete rows, you need to pay special attention to, such as a table, users in the use process after many delete rows, increasing the row after the operation, the values will not 1 with actual size, at that time, the List object, only in the form of subscript that just can have value, otherwise it will for null, see example:

Form code:


<form action="saysth.do" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstName" value="aaa" /></td>
<td><input name="users[0].lastName" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstName" value="ccc" /></td>
<td><input name="users[1].lastName" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[20].firstName" value="eee" /></td>
<td><input name="users[20].lastName" value="fff" /></td>
</tr>
</tbody>
</table>
</form>

At this time, userForm in Controller.getUsers() gets size of List as 21, and none of the 21 User objects will be null, but firstName and lastName in User objects from 2nd to 19th are null. Print result:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
4

6. Set binding:

Set is similar to List in that it is bound to an object and cannot be written directly into the parameters of the Controller method. However, when binding Set data, you must first add the corresponding number of model objects in the Set object.

Model code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
5

Controller code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
6

Form code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
7

It is basically similar to the List binding.

Need special remind is, if the largest values greater than Set size, would be thrown org. springframework. beans. InvalidPropertyException anomalies. Therefore, it is inconvenient to use it.

7. Map binding:

Map is the most flexible; it also needs to be bound to an object, not written directly into the parameters of the Controller method.

Model code:


public class User {
  private String firstName;
  private String lastName;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

}

public class UserMapForm {
  private Map<String, User> users;

  public Map<String, User> getUsers() {
    return users;
  }

  public void setUsers(Map<String, User> users) {
    this.users = users;
  }

}

Controller code:


<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
9

Form code:


<form action="saysth.do" method="post">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users['x'].firstName" value="aaa" /></td>
<td><input name="users['x'].lastName" value="bbb" /></td>
</tr>
<tr>
<td><input name="users['y'].firstName" value="ccc" /></td>
<td><input name="users['y'].lastName" value="ddd" /></td>
</tr>
<tr>
<td><input name="users['z'].firstName" value="eee" /></td>
<td><input name="users['z'].lastName" value="fff" /></td>
</tr>
</tbody>
</table>
</form>

Print result:


x: aaa - bbb
y: ccc - ddd
z: eee - fff

Related articles: